home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 3.6 KB | 138 lines |
- // WebComponent
- // A JFS component for loading data from URLs, via /dev/Web
- import java.awt.*;
- import java.net.*;
-
- public class WebComponent extends JFScomponent
- {
- java.applet.Applet ap; // root applet
- TextField url; // URL string to get
- Button get;
- StaticTextField type, size;
- Button view, save;
- FileReq savereq;
-
- byte urldata[] = null; // data read from URL
- String handler = null;
-
- void init(java.applet.Applet a)
- {
- ap = a;
- // create user interface
- setLayout(new BorderLayout());
- Panel top = new BorderPanel(2);
- top.setLayout(new BorderLayout());
- top.add("West",new Label("URL"));
- top.add("Center",url = new TextField("http://"));
- Panel getp = new Panel();
- getp.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getp.add(get = new Button("Get"));
- top.add("South",getp);
- add("North",top);
-
- Panel bot = new BorderPanel(2);
- bot.setLayout(new BorderLayout());
- Panel vsp = new Panel();
- vsp.setLayout(new BorderLayout());
- Panel vspl = new Panel();
- vspl.setLayout(new GridLayout(2,1));
- vspl.add(new Label("Content type"));
- vspl.add(new Label("Size"));
- vsp.add("West",vspl);
- Panel vspr = new Panel();
- vspr.setLayout(new GridLayout(2,1));
- vspr.add(type = new StaticTextField());
- vspr.add(size = new StaticTextField());
- vsp.add("Center",vspr);
- bot.add("North",vsp);
- Panel butp = new Panel();
- butp.setLayout(new FlowLayout(FlowLayout.RIGHT));
- butp.add(view = new Button("View"));
- butp.add(save = new Button("Save"));
- view.disable();
- save.disable();
- bot.add("South",butp);
- add("Center",bot);
- }
-
- public boolean action(Event evt, Object arg)
- {
- if (evt.target == get) {
- // Get button clicked.
- // Read from /dev/Web to get the data from this URL
- URL userurl = null;
- try userurl = new URL(url.getText());
- catch(MalformedURLException e) {
- new ErrorWindow("Invalid URL format");
- return true;
- }
- Message msg = new Message();
- msg.add("File","/dev/Web");
- msg.add("URL",userurl.toString());
- Message r = null;
- try r = client.send("Get", msg);
- catch(RequestException e) {
- new ErrorWindow("Get URL failed : "+e.getMessage());
- return true;
- }
-
- // Update URL information fields
- type.setText(r.find("Content-type"));
- client.currenttype = type.getText();
- size.setText(r.find("Content-length"));
- urldata = r.getdata();
- save.enable();
-
- // Try to find a handler class for the data
- view.disable();
- try handler = client.gethandler(type.getText());
- catch(RequestException e)
- handler = null;
- if (handler != null)
- view.enable();
- }
-
- else if (evt.target == save && savereq == null) {
- // Save URL data button clicked
- String dummy[] = {};
- savereq = new FileReq(client, this, "Save", "*/*", true, dummy);
- }
- else if (evt.target == savereq) {
- // File to save chosen
- if (((String)evt.arg).equals("Save")) {
- // save the current file
- try save(urldata, savereq.getfile(), savereq.gettype(),
- savereq.getversion(), savereq.multiversion());
- catch(RequestException e)
- new ErrorWindow("Could not save "+
- savereq.getfile()+" : "+
- e.getMessage());
- }
- savereq = null;
- }
-
- else if (evt.target == view) {
- // View URL data button clicked
- // Save the data to a temp file, and invoke a handler
- // program to view it.
- String tmp = "/tmp/"+System.currentTimeMillis();
- try save(urldata, tmp, type.getText(), 0, false);
- catch(RequestException e) {
- new ErrorWindow("Could not write to temp file : "+
- e.getMessage());
- return true;
- }
- new ProgramWindow(handler, client, ap, tmp, 0);
- try client.delete(tmp,0);
- catch(RequestException e);
- }
- return true;
- }
-
- Dimension wantedsize()
- {
- return new Dimension(400,220);
- }
- }
-
-